package com.ruoyi.safe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.enumeration.CommonDelFlag;
import com.ruoyi.safe.domain.TSpecialZone;
import com.ruoyi.safe.mapper.TSpecialZoneMapper;
import com.ruoyi.safe.service.ITSpecialZoneService;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 专区Service业务层处理
 *
 * @author zmk
 * @date 2025-03-24
 */
@Service
public class TSpecialZoneServiceImpl extends ServiceImpl<TSpecialZoneMapper, TSpecialZone> implements ITSpecialZoneService {
    @Autowired
    private TSpecialZoneMapper tSpecialZoneMapper;

    /**
     * 查询专区
     *
     * @param zoneId 专区主键
     * @return 专区
     */
    @Override
    public TSpecialZone selectTSpecialZoneByZoneId(Long zoneId) {
        return tSpecialZoneMapper.selectTSpecialZoneByZoneId(zoneId);
    }

    /**
     * 查询专区列表
     *
     * @param tSpecialZone 专区
     * @return 专区
     */
    @Override
    public List<TSpecialZone> selectTSpecialZoneList(TSpecialZone tSpecialZone) {
        //按条件查询
        List<TSpecialZone> tSpecialZones = tSpecialZoneMapper.selectTSpecialZoneList(tSpecialZone);
        //补齐所有父专区
        TSpecialZone specialZone = new TSpecialZone();
        specialZone.setDelFlag(tSpecialZone.getDelFlag());
        specialZone.setParentId(0L);
        List<TSpecialZone> tSpecialZonesParent = tSpecialZoneMapper.selectTSpecialZoneList(specialZone);
        List<Long> zondIdlist = tSpecialZones.stream().map(TSpecialZone::getZoneId).collect(Collectors.toList());
        tSpecialZonesParent.forEach(tSpecialZone1 -> {
            if (!zondIdlist.contains(tSpecialZone1.getZoneId())) {
                tSpecialZones.add(tSpecialZone1);
            }
        });
        return tSpecialZones;
    }

    /**
     * 新增专区
     *
     * @param tSpecialZone 专区
     * @return 结果
     */
    @Override
    @CacheEvict(value = "ZoneController", key = "'app-zone-list")
    public int insertTSpecialZone(TSpecialZone tSpecialZone) {
        determiningTheParentId(tSpecialZone);
        YouCannotAddTwoLayers(tSpecialZone);
        tSpecialZone.setCreateTime(DateUtils.getNowDate());
        return tSpecialZoneMapper.insertTSpecialZone(tSpecialZone);
    }
    //判断是否选择自己为父id

    private void determiningTheParentId(TSpecialZone tSpecialZone) {
        if (Objects.equals(tSpecialZone.getZoneId(), tSpecialZone.getParentId())) {
            throw new ServiceException("不能选择自己为父id");
        }
        ;
    }

    //判断不能添加两层
    private void YouCannotAddTwoLayers(TSpecialZone tSpecialZone) {
        if (tSpecialZone.getParentId() == null) {
            return;
        }
        if (tSpecialZone.getParentId() != 0) {
            TSpecialZone parentZone = tSpecialZoneMapper.selectTSpecialZoneByZoneId(tSpecialZone.getParentId());
            if (parentZone.getParentId() != 0) {
                throw new ServiceException("不能添加两层");
            }
        }
    }

    /**
     * 修改专区
     *
     * @param tSpecialZone 专区
     * @return 结果
     */
    @Override
    @CacheEvict(value = "ZoneController", key = "'app-zone-list'")
    public int updateTSpecialZone(TSpecialZone tSpecialZone) {
        YouCannotAddTwoLayers(tSpecialZone);
        determiningTheParentId(tSpecialZone);
        tSpecialZone.setUpdateTime(DateUtils.getNowDate());
        return tSpecialZoneMapper.updateTSpecialZone(tSpecialZone);
    }

    /**
     * 批量删除专区
     *
     * @param zoneIds 需要删除的专区主键
     * @return 结果
     */
    @Override
    @CacheEvict(value = "ZoneController", key = "'app-zone-list'")
    public int deleteTSpecialZoneByZoneIds(Long[] zoneIds) {
        return tSpecialZoneMapper.deleteTSpecialZoneByZoneIds(zoneIds);
    }

    /**
     * 删除专区信息
     *
     * @param zoneId 专区主键
     * @return 结果
     */
    @Override
    @CacheEvict(value = "ZoneController", key = "'app-zone-list'")
    public int deleteTSpecialZoneByZoneId(Long zoneId) {
        return tSpecialZoneMapper.deleteTSpecialZoneByZoneId(zoneId);
    }

    @Override
    public List<TSpecialZone> getMyZone(Long userId) {
        return tSpecialZoneMapper.getMyZone(userId);
    }

    @Override
    @Cacheable(value = "ZoneController", key = "'app-zone-list'")
    public List<TSpecialZone> selectTSpecialZoneAppList() {
        return tSpecialZoneMapper.selectList(new LambdaQueryWrapper<TSpecialZone>()
                .eq(TSpecialZone::getDelFlag, CommonDelFlag.NO_DEL.getCode()));
    }

    @Override
    @CacheEvict(value = "ZoneController", key = "'app-zone-list'")
    public boolean updateStatus(Integer delFlag, Long zoneId) {
        TSpecialZone tSpecialZone = tSpecialZoneMapper.selectTSpecialZoneByZoneId(zoneId);
        if (tSpecialZone == null) {
            throw new ServiceException("专区不存在");
        }
        Long parentId = tSpecialZone.getParentId();

        List<TSpecialZone> updateList = new ArrayList<>();
        if (Objects.equals(delFlag, CommonDelFlag.NO_DEL.getCode())) {
            //开
            if (parentId != 0L) {
                //开的是子zone 开启父zone
                TSpecialZone specialZoneP = new TSpecialZone();
                specialZoneP.setDelFlag(CommonDelFlag.NO_DEL.getCode());
                specialZoneP.setZoneId(parentId);
                updateList.add(specialZoneP);
            }
            tSpecialZone.setDelFlag(CommonDelFlag.NO_DEL.getCode());
            updateList.add(tSpecialZone);
        } else {
            //关
            //关的是父zone 关闭子zone
            if (parentId == 0L) {
                updateList = tSpecialZoneMapper.selectList(new LambdaQueryWrapper<TSpecialZone>().eq(TSpecialZone::getParentId, zoneId));
                updateList.forEach(e -> e.setDelFlag(CommonDelFlag.YES_DEL.getCode()));
            }
            tSpecialZone.setDelFlag(CommonDelFlag.YES_DEL.getCode());
            updateList.add(tSpecialZone);

        }
        ((ITSpecialZoneService) AopContext.currentProxy()).updateBatchById(updateList);
        return true;
    }
}
